package com.servlet;

import com.model.Moves;
import com.service.MovesService;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/MovesServlet")
public class MovesServlet extends HttpServlet {
	MovesService ms=new MovesService();
	HttpServletRequest request;
	HttpServletResponse response;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.request=request;
		this.response=response;
		String me=request.getParameter("method"); 
	if(me.equals("show")) {
		this.show(); 
	}
	if(me.equals("add")) {
		this.add(); 
	}
	if(me.equals("delete")) {
		this.delete(); 
	}
	if(me.equals("toupdate")) {
		this.toupdate(); 
	}
	if(me.equals("update")) {
		this.update(); 
	}
		if(me.equals("contain")) {
			this.contain();
		}
	}

	private void update() throws IOException {
		String mid=request.getParameter("mid");
		String file=request.getParameter("file");
        String author=request.getParameter("author");
        String time=request.getParameter("time");
		String hits=request.getParameter("hits");
		String statues=request.getParameter("statues");
        ms.update(mid,file,author,time,hits,statues);
	}

	private void toupdate() throws ServletException, IOException {
		String id=request.getParameter("id");
		Moves moves=ms.toupdate(id);
		request.setAttribute("move", moves);
		request.getRequestDispatcher("jsp/moves/update.jsp").forward(request, response);
		
	}

	private void delete() throws ServletException, IOException {
		String id=request.getParameter("id");
		ms.delete(id);
		this.show();
	}

	private void show() throws ServletException, IOException {
		List<Moves> moves=ms.show();
		request.setAttribute("moves",moves);
		request.getRequestDispatcher("jsp/moves/show.jsp").forward(request,response);
	}
	private void contain() throws ServletException, IOException {
		List<Moves> moves=ms.show();
		request.setAttribute("moves",moves);
		request.getRequestDispatcher("jsp/moves/contain.jsp").forward(request,response);
	}
	
	private void add() throws IOException {
		String file=request.getParameter("file");
		String author=request.getParameter("author");
		String time=request.getParameter("time");
		String hits=request.getParameter("hits");
		String statues=request.getParameter("statues");
        ms.add(file,author,time,hits,statues);
//        //response  对象向页面响应
        //response.getWriter().print("<script>parent.showStudent()</script>");
    }

}
